package com.xinzhitong.www.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhitong.www.exception.*;
import com.xinzhitong.www.exception.credentialcheckedrequire.CredentialNoCheckedException;
import com.xinzhitong.www.exception.credentialenroll.CredentialAlreadyExistsException;
import com.xinzhitong.www.exception.credentialenroll.CredentialEnrollErrException;
import com.xinzhitong.www.exception.credentialenroll.CredentialTimeIdErrException;
import com.xinzhitong.www.exception.credentialgeneral.NoCredentialGeneralException;
import com.xinzhitong.www.exception.credentiallocation.CredentialNoLocationException;
import com.xinzhitong.www.exception.credentialpicture.CredentialNoPictureException;
import com.xinzhitong.www.exception.credentialtime.CredentialNoTimeException;
import com.xinzhitong.www.pojo.ExamLocation;
import com.xinzhitong.www.pojo.ExamTime;
import com.xinzhitong.www.service.CredentialService;
import com.xinzhitong.www.utils.Message;
import com.xinzhitong.www.utils.MyPageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 证书控制器
 *
 * @author 钟伟良
 */
@CrossOrigin
@RestController
@RequestMapping("credential")
public class CredentialController {

    @Resource
    private CredentialService credentialService;

    /**
     * 证书id获取证书介绍
     *
     * @param id 证书id
     * @return 证书介绍
     */
    @RequestMapping(value = "introduction", method = RequestMethod.GET)
    @ResponseBody
    public Message introduction(@RequestParam("id") Integer id) {
        try {
            return Message.createSuc(credentialService.introduction(id));
        } catch (CredentialIntroductionException e) {
            e.printStackTrace();
            return Message.createErr("此证书没有介绍");
        }
    }

    /**
     * 证书id查找证书要求
     *
     * @param credentialId 证书id
     * @return 证书要求
     */
    @RequestMapping(value = "request", method = RequestMethod.GET)
    @ResponseBody
    public Message request(@RequestParam("credentialId") Integer credentialId) {
        try {
            return Message.createSuc(credentialService.request(credentialId));
        } catch (CredentialRequestException e) {
            e.printStackTrace();
            return Message.createErr("此证书没有要求");
        }
    }

    /**
     * 考证id查找考证地址
     *
     * @param credentialId 考证id
     * @return 考证地址
     * @throws CredentialLocationException 考证地址异常
     */
    @RequestMapping(value = "location", method = RequestMethod.GET)
    @ResponseBody
    public Message location(@RequestParam("credentialId") Integer credentialId) throws CredentialLocationException {
        try {
            List<ExamLocation> x = credentialService.location(credentialId);
            Integer y = x.size();
            List<Map<String, Object>> z = new ArrayList<Map<String, Object>>();
            for (ExamLocation examLocation : x) {
                Map<String, Object> yyy = new HashMap<>();
                yyy.put("locationId", examLocation.getId());
                yyy.put("location", (examLocation.getProvince() + examLocation.getCity() + examLocation.getArea() + examLocation.getAddress()));
                z.add(yyy);
            }
            return Message.createSuc(z);
        } catch (CredentialNoLocationException e) {
            e.printStackTrace();
            return Message.createErr("无考证地址");
        }
    }

    /**
     * 考证id返回考证时间
     *
     * @param locationId 考证地点id
     * @return 考证场次
     * @throws CredentialTimeException 考证时间异常
     */
    @RequestMapping(value = "time", method = RequestMethod.GET)
    @ResponseBody
    public Message time(@RequestParam("locationId") Integer locationId) throws CredentialTimeException {
        try {
            List<ExamTime> x = credentialService.time(locationId);
            Integer y = x.size();
            List<Map<String, Object>> z = new ArrayList<Map<String, Object>>();
            for (ExamTime examTime : x) {
                Map<String, Object> yyy = new HashMap<>();
                yyy.put("timeId", examTime.getId());
                yyy.put("time", (examTime.getStartTime() + "--" + examTime.getEndTime()));
                z.add(yyy);
            }
            return Message.createSuc(z);
        } catch (CredentialNoTimeException e) {
            e.printStackTrace();
            return Message.createErr("考证地点无考证时间");
        }
    }

    /**
     * 查询报名要求
     *
     * @param credentialId 考证id
     * @return 报名要求
     */
    @RequestMapping(value = "checked", method = RequestMethod.GET)
    @ResponseBody
    public Message checked(@RequestParam("credentialId") Integer credentialId) {
        try {
            return Message.createSuc(credentialService.checked(credentialId));
        } catch (CredentialNoCheckedException e) {
            e.printStackTrace();
            return Message.createErr("无报名要求");
        }
    }

    /**
     * 查询图片
     *
     * @param credentialId 考证id
     * @return 考证图片
     */
    @RequestMapping(value = "picture", method = RequestMethod.GET)
    @ResponseBody
    public Message picture(@RequestParam("credentialId") Integer credentialId) {
        try {
            return Message.createSuc(credentialService.picture(credentialId));
        } catch (CredentialNoPictureException e) {
            e.printStackTrace();
            return Message.createErr("暂无图片");
        }
    }

    /**
     * 获取证书概览
     *
     * @param professionId 工种id
     * @param pageNo       页码
     * @param pageSize     页大小
     * @return 证书概览
     */
    @RequestMapping(value = "general", method = RequestMethod.GET)
    @ResponseBody
    public Message general(@RequestParam(value = "professionId") Integer professionId,
                           @RequestParam("pageNo") int pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<Map<String, Object>> x = credentialService.general(professionId);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(x);
            MyPageInfo myPageInfo = new MyPageInfo(pageInfo);
            return Message.createSuc(myPageInfo);
        } catch (NoCredentialGeneralException e) {
            e.printStackTrace();
            return Message.createErr("工种暂无证书");
        }
    }

    /**
     * 考证报名
     *
     * @param timeId 考证时间id
     * @return 报名成功或者失败信息
     * @throws CredentialEnrollException 报名失败信息
     */
    @RequestMapping(value = "enroll", method = RequestMethod.POST)
    @ResponseBody
    public Message enroll(@RequestParam(value = "timeId") Integer timeId) throws CredentialEnrollException {
        try {
            credentialService.examTime(timeId);
            return Message.createSuc("报名成功");
        } catch (CredentialEnrollErrException e) {
            e.printStackTrace();
            return Message.createErr("报名失败");
        } catch (CredentialTimeIdErrException e) {
            e.printStackTrace();
            return Message.createErr("考证时间输入错误");
        } catch (CredentialAlreadyExistsException e) {
            e.printStackTrace();
            return Message.createErr("重复报名");
        }
    }

}
